package com.rest.entity;

import java.util.Map;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

/**
 * 权限信息表
 */
@Entity
@Table(name = "acct_authority", catalog = "study")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class AcctAuthority extends IdEntity {

	/**
	 * SpringSecurity中默认的角色/授权名前缀.
	 */
	public static final String AUTHORITY_PREFIX = "ROLE_";

	private String name;

	private String modelId;// 编号，可以看出菜单级别、推知父级别

	private String modelName;// 模块名称，附加在"浏览xxx"记录后面

	private String iconUrl;// 小图标文件名称

	private String url;// 点击url，二级菜单用

	public AcctAuthority() {
	}

	public AcctAuthority(Map<String, Object> map) {
		this.id = (Integer) map.get("id");
		this.name = (String) map.get("name");
		this.modelId = (String) map.get("model_id");
		this.modelName = (String) map.get("model_name");
		this.url = (String) map.get("url");
		this.iconUrl = (String) map.get("icon_url");
	}

	public AcctAuthority(Integer id, String name, String modelId,
			String modelName, String url, String iconUrl) {
		this.id = id;
		this.name = name;
		this.modelId = modelId;
		this.modelName = modelName;
		this.url = url;
		this.iconUrl = iconUrl;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getModelId() {
		return modelId;
	}

	public void setModelId(String modelId) {
		this.modelId = modelId;
	}

	public String getModelName() {
		return modelName;
	}

	public void setModelName(String modelName) {
		this.modelName = modelName;
	}

	@Transient
	public String getPrefixedName() {
		return AUTHORITY_PREFIX + name;
	}

	public String getIconUrl() {
		return iconUrl;
	}

	public void setIconUrl(String iconUrl) {
		this.iconUrl = iconUrl;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	public String toJsString() {
		if (StringUtils.isNotEmpty(this.modelId)) {
			String js = "var m_" + id + "={'id':" + id + ",'name':'"
					+ modelName + "','modelId':'" + modelId + "',";
			if (this.modelId.length() == 2) {
				js += "'img':'" + this.iconUrl + "'};";
			} else {
				js += "'url':'" + this.url + "'};";
			}
			return js;
		}
		return "";
	}
}
